package com.wht.controller;

import com.wht.domain.Music;
import com.wht.domain.Music_Singer;
import com.wht.domain.Singer;
import com.wht.service.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author wanghaotian
 * time:2022/11/28,0028 11:01
 * description:
 */

@WebServlet("/AddMusicController")
public class AddMusicController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String album = req.getParameter("album");
        Music music = new Music(null,name,album,null);
        String[] sids = req.getParameterValues("sid");
        AddMusicService addMusicService = new AddMusicServiceImpl();
        AddMusicSingerService addMusicSingerService = new AddMusicSingerServiceImpl();
        addMusicService.addMusic(music);
        for (String sid : sids) {
            int singerId = Integer.parseInt(sid);
            Music_Singer musicSinger = new Music_Singer(music.getMid(), singerId);
            addMusicSingerService.addMusicSinger(musicSinger);
        }
        resp.sendRedirect("/FindAllMusicController");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}